import { observable, action } from 'mobx-miniprogram'

import { http } from '../utils/request'

export const store = observable({
  bottomHeight: 0,
  cart: JSON.parse(wx.getStorageSync('cart') || '[]'),
  address: JSON.parse(wx.getStorageSync('address') || '{}'),
  token: wx.getStorageSync('token'), //登录token
  userInfo: JSON.parse(wx.getStorageSync('userInfo') || '{}'),
  redirectInfo: null,

  get cartCount() {
    let count = 0
    this.cart.map(item => {
      count += item.goods_count
    })
    return count
  },

  get checkedGoodsCount() {
    return this.cart.filter(x => x.goods_state).reduce((total, item) => total += item.goods_count, 0)
  },

  get totalPrice() {
    return this.cart.filter(x => x.goods_state).reduce((total, item) => total += item.goods_count * item.goods_price, 0)
  },

  get addressStr() {
    return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
  },

  setBottomHeight: action(function(height) {
    this.bottomHeight = height
  }),

  // 加入购物车
  addToCart: action(function(goods) {
    const findResult = this.cart.find(item => item.goods_id === goods.goods_id)
    // 查找store里面是否有该商品，无则push 有则数量 +1
    if(!findResult) {
      this.cart.push(goods)
      // 增加购物车数量
    } else {
      findResult.goods_count++
    }
    // 持久化存储购物车中的商品
    wx.setStorageSync('cart', JSON.stringify(this.cart))
  }),

  // 更新商品勾选状态
  updateGoodsState: action(function(goods){
    const findResult = this.cart.find(item => item.goods_id === goods.goods_id)
    if(findResult) {
      findResult.goods_state = goods.goods_state
      // 更新持久化本地存储信息
      wx.setStorageSync('cart', JSON.stringify(this.cart))
    }
    // console.log(this.cart)
  }),
  
  // 更新商品数量
  updateGoodsCount: action(function(goods) {
    const findResult = this.cart.find(item => item.goods_id === goods.goods_id)
    if (findResult) {
      findResult.goods_count = goods.goods_count
       // 更新持久化本地存储信息
       wx.setStorageSync('cart', JSON.stringify(this.cart))
    }
  }),

  // 删除商品
  deleteGoods: action(function(goods_id) {
    const index = this.cart.findIndex(item => item.goods_id === goods_id)
    if (index !== -1) {
      this.cart.splice(index, 1)
      // 更新持久化本地存储信息
      wx.setStorageSync('cart', JSON.stringify(this.cart))
    }
  }),

  // 添加收货地址
  updateAddress: action(function(address) {
    this.address = address
    wx.setStorageSync('address', JSON.stringify(address))
  }),

  // 全选 / 反选
  updateAllGoodsState: action(function(state) {
    this.cart.map(x => x.goods_state = state)
    wx.setStorageSync('cart', JSON.stringify(this.cart))
  }),

  // 保存用户信息
  updateUserInfo: action(function(info) {
    this.userInfo = info
    wx.setStorageSync('userInfo', JSON.stringify(info))
  }),

  // 保存token
  updateUserToken: action(function(token) {
    this.token = token
    wx.setStorageSync('token', token)
  }),

  // 保存登录前重定向数据
  updateRedirectInfo: action(function(info) {
    this.redirectInfo = info
  }),

  // 需要请求头的token的http请求（本想在utils/request.js写的，但是在组件/页面外不懂能不能用mbox）
  tokenHttp: action(function() {
    http.beforeRequest = (options) => {
      if(options.url.indexOf('/my/') !== -1) {
        options.header = {
          Authorization: this.token
        }
      }
    }
    return http
  })

})